package com.whc.friend.service.impl;


import com.whc.api.client.UserClient;
import com.whc.api.dto.UserPageVo;
import com.whc.common.domain.Result;
import com.whc.friend.domain.entity.Friend;
import com.whc.friend.domain.vo.FriendVo;
import com.whc.friend.mapper.FriendMapper;
import com.whc.friend.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserClient userClient;


    public void addFriend(long pid, long fid) {
        Friend friend = new Friend();
        friend.setFid(fid);
        friend.setPid(pid);
        friend.setType(1);
        friendMapper.insert(friend);
    }

    @Override
    public List<FriendVo> getAllFriends(Long pid) {
        List<Long> userIds = friendMapper.getAllByPid(pid);
        Result<List<UserPageVo>> userByIds = userClient.getUserByIds(userIds);
        List<UserPageVo> users = userByIds.getData();
        ArrayList<FriendVo> friendVos = new ArrayList<>();
        if(users==null)return friendVos;
        for(UserPageVo u:users){
            FriendVo friendVo = new FriendVo();
            friendVo.setFName(u.getUsername());
            friendVo.setFPhoto(u.getPhoto());
            friendVo.setId(u.getId());
            friendVos.add(friendVo);
        }
        return friendVos;
    }
}
